<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PORTAL ADMINISTRATION</title>

<link href="../dijit/themes/tundra/tundra.css" type="text/css" rel="stylesheet"/>
<link href="../dijit/themes/tundra/tundra_rtl.css" type="text/css" rel="stylesheet"/>

<script type="text/javascript" src="../dojo/dojo.js" djConfig="isDebug: true, parseOnLoad: true, usePlainJson: true "></script>
<script type="text/javascript" src="../dijit/dijit.js"></script>
<script type="text/javascript" src="../dijit/dijit.js"></script>

<script type="text/javascript" src="./js/jsonrpc-min.js"></script>
<script type="text/javascript" src="./js/page-admin.js"></script>
<style type="text/css">
	@import "../dojo/resources/dojo.css";
	@import "../css/dijitTests.css";
	@import "../dojox/grid/_grid/tundraGrid.css";

	.dojoContentPane {
		padding:1em;
	}
	
	#main_container {
	    position: relative;
		border: 1px	solid #bfbfbf; 
		float: left; 
		width: 100%; 
		height:500px;
	}
	
	
</style>
	

<script type="text/javascript">
	dojo.require("dijit.layout.SplitContainer");
	dojo.require("dijit.layout.ContentPane");
	dojo.require("dijit.layout.StackContainer");	
	dojo.require("dijit.layout.ContentPane");
	dojo.require("dojo.data.ItemFileWriteStore");
	dojo.require("dijit.Tree");
	dojo.require("dojox.grid.Grid");
	dojo.require("dojox.grid._data.model");
	dojo.require("dijit.Dialog");
	dojo.require("dijit.form.Button");
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.DateTextBox");
	dojo.require("dijit.form.TimeTextBox");	
	dojo.require("dijit.form.ComboBox");
	//dojo.require("dijit.ColorPalette");
	dojo.require("dijit.Toolbar");
	dojo.require("dijit.Menu");	

	dojo.require("dojo.parser");	// scan page for widgets and instantiate them

	var	jsonrpc = new JSONRpcClient("/JSON-RPC");

	//
	// Grid layout
	//    
	var layoutColumns = [
			// view 0
			{ type: 'dojox.GridRowView', width: '20px' },
			// view 1
			{ cells: [[{ name: "N.", get: getRow, width: 5}]], noscroll: true},
			// view 2
			{ cells: [[
				{ name: 'NAME', field: 'name' },
				{ name: 'SIZE', field: 'size' },
				{ name: 'TYPE', field: 'typeName' }
			]]}
		];

	
	//
	// grid function
	// 
	function getRow(inRowIndex) { return ' ' + inRowIndex;	}
	
	//
	//
	//
	function fillTableItems( context ) {
		try
		{
		  // Call a Java method on the server
		  var result = jsonrpc.portal.getTables();
		  
		  var pInfo =  {parent:context.item, attribute:"children"};
		  
		  for( var i=0; i<result.length; ++i ) {
		
			  //console.debug(result[i].name);
	  		  context.store.newItem( {id:result[i].name, name:result[i].schema+'.'+result[i].name,action:result[i].action},pInfo);
			    
		  }
		}
		catch(e)
		{
		  console.error(e);
		}
	}

	//
	//
	//
	function fillServiceItems( context ) {
		try
		{
		  // Call a Java method on the server
		  var result = jsonrpc.portal.getServices();
		  
		  var pInfo =  {parent:context.item, attribute:"children"};
		  
		  for( var i=0; i<result.length; ++i ) {
		
			  //console.debug(result[i].name);
	  		  context.store.newItem( {id:result[i].id, name:result[i].name},pInfo);
			    
		  }
		}
		catch(e)
		{
		  console.error(e);
		}
	}
	//
	//
	//
	function fillPanelsItems( context ) {
		try
		{
		  // Call a Java method on the server
		  /*
		  var result = jsonrpc.portal.getServices();
		  
		  var pInfo =  {parent:context.item, attribute:"children"};
		  
		  for( var i=0; i<result.length; ++i ) {
		
			  //console.debug(result[i].name);
	  		  context.store.newItem( {id:result[i].id, name:result[i].name},pInfo);
			    
		  }
		  */
		}
		catch(e)
		{
		  console.error(e);
		}
	}
	
//
// set global variable 'templates' and fill main tree view
//
//@context object {item: tree parent item, store: data store}
function fillTemplateItems( context ) {
	try	{
	
		templates = jsonrpc.portal.getTemplates();
		
		for( var i=0 ; i<templates.length; ++i ) {
			//console.debug( "template.var "+dojo.toJson(templates[i]) );			
			templateStore.newItem( {name:templates[i].name, index:i } );
		}				
	  			
		var pInfo =  {parent:context.item, attribute:"children"};
		 
		for( var i=0; i<templates.length; ++i ) {		
			//console.debug(templates[i].name);
			context.store.newItem( {id:templates[i].name, name:templates[i].name, action:function(ctx){openTemplate(ctx)} },pInfo);
		}
	  
	} catch(e) {
	  console.error(e);
	}
}

//
//
//
function openTemplate(ctx) {

	var name = ctx.store.getValue( ctx.item, 'name');
	console.debug( "openTemplate "+name );			
	
	var win = window.open( "/osgip/template/"+ name,  
							name, 
							"status=0,menubar=0,resizable=1,scrollbars=1,addressbar=0");
}

	
//
//
//
function fillColumnItems( context ) {
	try
	{
	  var schema = context.store.getValue( context.item, "schema" );
	  var table = context.store.getValue( context.item, "id" );
	  
	  console.debug( "schema=" + schema + " table=" + table ); 

	  // Call a Java method on the server
	  var rs = jsonrpc.portal.getColumns( schema, table );
	  
	  var columnModel = new dojox.grid.data.Objects(null, rs);
         columnGrid.setModel(columnModel);
         columnGrid.refresh();
	  
	}
	catch(e)
	{
	  console.error(e);
	}
}
	
//
//
//
function tree_onClick(item){
	var stackContainer = dijit.byId("myStackContainer");
	
	var store = dijit.byId("adminTree").store;
	
	selectedItem = item;
	console.debug( "selectItem " + item.name );
	//dojo.byId('selected').innerHTML= item ? adminTreeModel.getLabel(item) : "";
	
	var action = store.getValue(item,'action');
	console.debug( "action=" + action + "typeof=" + typeof(action) );
	if( null!=action ) {
		
		var children = store.getValue(item,'children');
		
		if( children ) {
			console.debug( "item already loaded " );
		}
		else {			
		
			var context = {store:store, item:item };
			console.debug( "action execution" );
			if( typeof(action)=='string' ) action = eval( action );
			if( typeof(action)=='function' ) action( context );
		}
		
		stackContainer.selectChild("table_grid");
		
	}
	else {
		stackContainer.selectChild("empty_panel");
	}
	
}

//
//
//
function selectTemplate( tpl ) {
		console.debug( "template "+tpl);	
		
		var oTpl = 	templateStore.fetch( {query:{name: tpl}, onComplete: PageAdmin.createPageVars}  );
		if( !oTpl ) {
			console.warning( "template "+tpl+"not found!");
		}
}

//
//
//
dojo.addOnLoad(function(){
	try	{
		panels = jsonrpc.portal.getPanelNames();
		
		
		// fill panel store
		for( i=0 ; i<panels.length; ++i ) {
			console.debug( "panel "+dojo.toJson(panels[i]) );			
			panelsStore.newItem( {name:panels[i]} );
		}
		
		// Fill template node
		adminTreeModel.fetch( {query:{id: 'tpl'}, onComplete: function(items, request) {
			console.debug( "item "+items[0].name );		
			fillTemplateItems( {item:items[0], store:adminTreeModel} );
			
		}}  );	
		
		PageAdmin.onLoad();

/*
		dojo.connect(menu, "_openMyself", this, function(e){
			console.debug("second");
		});
*/		
	} catch(e) {
		console.error(e);
	}	
		
});

</script>
	
</head>

<body class="tundra">

<div dojoType="dojo.data.ItemFileWriteStore" jsId="adminTreeModel" url="data/adminTreeData.json"></div>
<div dojoType="dojo.data.ItemFileWriteStore" jsId="templateStore" url="data/templates.json"></div>
<div dojoType="dojo.data.ItemFileWriteStore" jsId="panelsStore" url="data/panels.json"></div>

<!-- MENU BAR -->
<div id="menubar" dojoType="dijit.Toolbar" class="menuBar">

		<div dojoType="dijit.form.DropDownButton">
			<span>File</span>
			<div dojoType="dijit.Menu">
				<div dojoType="dijit.MenuItem" onClick="PageAdmin.openDialog()">New Page</div>
			</div>
		</div>
		<div dojoType="dijit.form.DropDownButton">
			<span>Help</span>
			<div dojoType="dijit.Menu">
				<div dojoType="dijit.MenuItem">Help Topics</div>
				<div dojoType="dijit.MenuItem">About Dijit</div>
			</div>
		</div>

</div>	


<!--  MAIN CONTAINER  -->
<div id="main_container" 
	dojoType="dijit.layout.SplitContainer"
	orientation="horizontal"
	sizerWidth="7"
	activeSizing="true"
	style="width: 100%; height: 500px;"
	>
		
	<div dojoType="dijit.layout.ContentPane" sizeMin="20" sizeShare="20">
		
		<div dojoType="dijit.Tree" 
			 id="adminTree" 
			 label="root" 
			 store="adminTreeModel" 
			 onClick="tree_onClick" 
			 labelAttr="name" 
			 >
		</div>

	</div>
	
	<div id="myStackContainer" dojoType="dijit.layout.StackContainer">	
		<div id="empty_panel" dojoType="dijit.layout.ContentPane" sizeMin="50" sizeShare="50">
		</div>
	  	
		<div id="table_grid" dojoType="dijit.layout.ContentPane" sizeMin="50" sizeShare="50">
		
			<div id="divColumnGrid" 
					dojoType="dojox.Grid" 
					jsId="columnGrid"
					elasticView="2" 
					structure="layoutColumns">
			</div>
		</div>
	</div>
	
</div><!-- END OF main_container -->


<!-- DIALOG ADD NEW PAGE -->
<div dojoType="dijit.Dialog" id="pageDialog" 
				title="Add/Update Page"	
				execute="PageAdmin.submitPageDialog(arguments[0])">

	<input dojoType="dijit.form.TextBox" type="hidden" name="action" id="action"></input>

	<div dojoType="dijit.layout.ContentPane" >
		<table>
		<tr>
			<td><label for="name">Page Name: </label></td>
			<td><input dojoType=dijit.form.TextBox type="text" name="name" id="name"></td>
		</tr>
		<tr>
			<td><label for="template">Templates: </label></td>
			<td><input id="template" name="template" dojoType=dijit.form.ComboBox store=templateStore onChange="selectTemplate"/></td>
		</tr>
		<tr>
			<td colspan="2">
				<div id="pageVarsDiv"></div>
			</td>
		</tr>

		<tr>
		<td><button type="submit" dojoType="dijit.form.Button" id="submit">OK</button></td>
		<td><button dojoType="dijit.form.Button" id="close" onClick="PageAdmin.closeDialog()">Close</button></td>
		</tr>

		</table>
	</div>


</div>

<div style="clear:left;">

</body>
</html>
